/*
 * 单例配置类
 *
 * 存放管理系统的所有配置信息
 *
 */
#pragma once

#include "util.hh"

namespace cloud
{
class config
{
public:
    static config& instance()
    {
        static config cfg;
        return cfg;
    }

                size_t hot_time()        { return _hot_time;        }
    const std::string& search_prefix()   { return _search_prefix;   }
    const std::string& record_prefix()   { return _record_prefix;   }
    const std::string& upload_prefix()   { return _upload_prefix;   }
    const std::string& download_prefix() { return _download_prefix; }
    const std::string& filelist_prefix() { return _filelist_prefix; }
    const std::string& back_dir()        { return _back_dir;        }
    const std::string& pack_dir()        { return _pack_dir;        }
    const std::string& pack_suffix()     { return _pack_suffix;     }
    const std::string& record_file()     { return _record_file;     }
    const std::string& sslcert_file()    { return _sslcert_file;    }
    const std::string& sslkey_file()     { return _sslkey_file;     }
              uint16_t server_port()     { return _server_port;     }
    const std::string& web_root_dir()    { return _web_root_dir;    }
    const std::string& sql_user()        { return _sql_user;        }
    const std::string& sql_passwd()      { return _sql_passwd;      }
    const std::string& sql_host()        { return _sql_host;        }
              uint16_t sql_port()        { return _sql_port;        }
    const std::string& sql_db()          { return _sql_db;          }
    const std::string& persist_type()    { return _persist_type;    }
                size_t thread_num()      { return _thread_num;      }
                size_t queue_size()      { return _queue_size;      }

private:
    config()
    {
        util::file conf("cloud.json");

        if (!conf.exists())
            throw except("cloud.json is not found");

        util::json js;
        js.deserialize(conf.read());

        _hot_time        = js.get<size_t>     ("hot_time");
        _search_prefix   = js.get<std::string>("search_prefix");
        _record_prefix   = js.get<std::string>("record_prefix");
        _upload_prefix   = js.get<std::string>("upload_prefix");
        _download_prefix = js.get<std::string>("download_prefix");
        _filelist_prefix = js.get<std::string>("filelist_prefix");
        _back_dir        = js.get<std::string>("back_dir");
        _pack_dir        = js.get<std::string>("pack_dir");
        _pack_suffix     = js.get<std::string>("pack_suffix");
        _record_file     = js.get<std::string>("record_file");
        _sslcert_file    = js.get<std::string>("sslcert_file");
        _sslkey_file     = js.get<std::string>("sslkey_file");
        _server_port     = js.get<int>        ("server_port");
        _web_root_dir    = js.get<std::string>("web_root_dir");
        _sql_user        = js.get<std::string>("sql_user");
        _sql_passwd      = js.get<std::string>("sql_passwd");
        _sql_host        = js.get<std::string>("sql_host");
        _sql_port        = js.get<int>        ("sql_port");
        _sql_db          = js.get<std::string>("sql_db");
        _persist_type    = js.get<std::string>("persist_type");
        _thread_num      = js.get<size_t>     ("thread_num");
        _queue_size      = js.get<size_t>     ("queue_size");

        easylog::info("config instance created");
    }

    config(const config&) = delete;
    config& operator=(const config&) = delete;

private:
    size_t      _hot_time;
    std::string _search_prefix;
    std::string _record_prefix;
    std::string _upload_prefix;
    std::string _download_prefix;
    std::string _filelist_prefix;
    std::string _back_dir;
    std::string _pack_dir;
    std::string _pack_suffix;
    std::string _record_file;
    std::string _sslcert_file;
    std::string _sslkey_file;
    uint16_t    _server_port;
    std::string _web_root_dir;
    std::string _sql_user;
    std::string _sql_passwd;
    std::string _sql_host;
    uint16_t    _sql_port;
    std::string _sql_db;
    std::string _persist_type;
    size_t      _thread_num;
    size_t      _queue_size;
};
}
